﻿using Choucius.Boilerplate9.Controllers;
using Choucius.Boilerplate9.InboundPlasticContainerLogs;
using Choucius.Boilerplate9.OutboundPlasticContainerLogs;
using Choucius.Boilerplate9.PlasticContainers;
using Choucius.Boilerplate9.Web.Models.Home;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks;
using Choucius.Boilerplate9.Web.Models.PlasticContainers;

namespace Choucius.Boilerplate9.Web.Controllers
{
    public class PlasticContainerStatisticsController : Boilerplate9ControllerBase
    {
        private readonly PlasticContainerAppService _plasticContainerAppService;
        private readonly InboundPlasticContainerLogAppService _inboundPlasticContainerLogAppService;
        private readonly OutboundPlasticContainerLogAppService _outboundPlasticContainerLogAppService;

        public PlasticContainerStatisticsController(PlasticContainerAppService plasticContainerAppService, InboundPlasticContainerLogAppService inboundPlasticContainerLogAppService, OutboundPlasticContainerLogAppService outboundPlasticContainerLogAppService)
        {
            _plasticContainerAppService = plasticContainerAppService;
            _inboundPlasticContainerLogAppService = inboundPlasticContainerLogAppService;
            _outboundPlasticContainerLogAppService = outboundPlasticContainerLogAppService;
        }

        public async Task<IActionResult> IndexAsync()
        {
            var plasticContainer = _plasticContainerAppService.GetStatusCount();
            ViewBag.pc = plasticContainer.Total;
            ViewBag.pc1 = plasticContainer.Initialization;
            ViewBag.pc2 = plasticContainer.InPackStock;
            ViewBag.pc3 = plasticContainer.OutPackStock;
            ViewBag.pc4 = plasticContainer.ProductBinding;
            ViewBag.pc5 = plasticContainer.OilCoated;
            ViewBag.pc6 = plasticContainer.InProductStock;
            ViewBag.pc1p = plasticContainer.Total == 0 ? "0%" : (plasticContainer.Initialization * 100 / plasticContainer.Total).ToString() + "%";
            ViewBag.pc2p = plasticContainer.Total == 0 ? "0%" : (plasticContainer.InPackStock * 100 / plasticContainer.Total).ToString() + "%";
            ViewBag.pc3p = plasticContainer.Total == 0 ? "0%" : (plasticContainer.OutPackStock * 100 / plasticContainer.Total).ToString() + "%";
            ViewBag.pc4p = plasticContainer.Total == 0 ? "0%" : (plasticContainer.ProductBinding * 100 / plasticContainer.Total).ToString() + "%";
            ViewBag.pc5p = plasticContainer.Total == 0 ? "0%" : (plasticContainer.OilCoated * 100 / plasticContainer.Total).ToString() + "%";
            ViewBag.pc6p = plasticContainer.Total == 0 ? "0%" : (plasticContainer.InProductStock * 100 / plasticContainer.Total).ToString() + "%";

            // 统计卡片
            var cardList = new List<StatisticInboundPlasticContainerViewModel>();
            var statisticItems = _plasticContainerAppService.InboundStatisticByPlasticContainerNameAsync();
            foreach (var item in statisticItems.Result)
            {
                var card = new StatisticInboundPlasticContainerViewModel { PlasticContainerName = item.Key, InboundCount = item.Value };
                cardList.Add(card);
            }
            ViewBag.Cards = cardList;

            var inboundPlasticContainer = await _inboundPlasticContainerLogAppService.GetInboundStatistics();
            var outboundPlasticContainer = await _outboundPlasticContainerLogAppService.GetOutboundStatistics();
            // 示例数据
            var chartData = new HomeViewModel
            {
                Labels = new List<string>(),
                InboundPlasticContainerCount = new List<int>(),
                OutboundPlasticContainerCount = new List<int>(),
                //InboundRubberFrameCount = new List<int>(),
                //OutboundRubberFrameCount = new List<int>()
            };
            for (int i = 6; i >= 0; i--)
            {
                string date = DateTime.Now.Date.AddDays(-i).ToShortDateString();
                chartData.Labels.Add(date);

                int inboundPlasticContainerCount = 0;
                var inboundPlasticContainerData = inboundPlasticContainer.FirstOrDefault(x => x.TimeInterval == date);
                if (inboundPlasticContainerData != null)
                {
                    inboundPlasticContainerCount = inboundPlasticContainerData.TotalCount;
                }
                chartData.InboundPlasticContainerCount.Add(inboundPlasticContainerCount);

                int outboundPlasticContainerCount = 0;
                var outboundPlasticContainerData = outboundPlasticContainer.FirstOrDefault(x => x.TimeInterval == date);
                if (outboundPlasticContainerData != null)
                {
                    inboundPlasticContainerCount = outboundPlasticContainerData.TotalCount;
                }
                chartData.OutboundPlasticContainerCount.Add(outboundPlasticContainerCount);
            }
            return View(chartData);
        }
    }
}
